#include "FrameBuffer.h"

const int   TEXTURE_WIDTH   = SCREEN_W;  // NOTE: texture size cannot be larger than
const int   TEXTURE_HEIGHT  = SCREEN_H;  // the rendering window size in non-FBO mode

FrameBuffer::FrameBuffer(void)
{
}

FrameBuffer::~FrameBuffer(void)
{
}

int FrameBuffer::GetID()
{
	return fboId;
}

void FrameBuffer::Init()
{
	glGenFramebuffers(1, &fboId);
	glGenRenderbuffers(1, &depthBufferId);
	GLuint textureId;
	glGenTextures(1, &textureId);
	texture.textureID = textureId;
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_W, SCREEN_H, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

	// bind renderbuffer and create a 16-bit depth buffer
	// width and height of renderbuffer = width and height of
	// the texture
	glBindRenderbuffer(GL_RENDERBUFFER, depthBufferId);
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16,
		SCREEN_W, SCREEN_H);
	// bind the framebuffer
	glBindFramebuffer(GL_FRAMEBUFFER, fboId);
	// specify texture as color attachment
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
	GL_TEXTURE_2D, textureId, 0);
	// specify depth_renderbufer as depth attachment
	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
	GL_RENDERBUFFER, depthBufferId);
	// switch back to window-system-provided framebuffer
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
		LOGI("Successfuly loaded Frame Buffer" );
	else
		LOGI("UnSuccessfuly loaded Frame Buffer");
}

void FrameBuffer::Update()
{

}